<?php
//
//
//
//
include('/var/www/html/new/com.inc.php');
include('/var/www/html/new/d/class/abc/Product_H.class.php');
include('/var/www/html/new/d/module/common/Db.class.php');
include('/var/www/html/new/d/class/abc/Syslog.class.php');
$dbConf = include '/var/www/html/new/d/module/common/db.conf.php';// 远端服务器配置信息

$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  =  new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));


\PFT\Db::Conf($dbConf['remote_1']);
$ph = new abc777\Product_H(\PFT\Db::Connect());

write_logs(json_encode($_REQUEST));


if(!isset($_REQUEST['Action']) || $_REQUEST['Action']=='submit'){
	
	// if($_REQUEST['RoundID']!=3606 && $_REQUEST['RoundID']!=3607 && $_REQUEST['RoundID']!=3680) Response("其他场次暂不出售",1062);
	// if($_REQUEST['RoundID']==3610) Response("3610其他场次暂不出售",1062);
	
	// 获取场次信息
	$dbRows  = \PFT\Db::fetch("select use_date,bt,et from pft_round where id=".$_REQUEST['RoundID']." limit 1");
	$rinfo   = $dbRows['use_date'].' '.$dbRows['bt'].'-'.$dbRows['et'];
	$rinfo_1 = $dbRows['bt'];
	
	if($_REQUEST['AreaID']>0){
		
		$seats_tmp = $ph->dync_add_seat($_REQUEST['RoundID'], $_REQUEST['VenueID'], $_REQUEST['AreaID'], $_REQUEST['Tnum'], $_REQUEST['Fid']);
		if(count($seats_tmp)==0) Response("场馆:{$_REQUEST['VenueID']},分区:{$_REQUEST['AreaID']},场次:{$_REQUEST['RoundID']},预定:{$_REQUEST['Tnum']}",1062);	
	
		$dync_ids = implode(',', $seats_tmp);// 获取座位信息
		$dbRows = \PFT\Db::fetchAll("select d.*,r.custom_num from pft_roundseat_dyn d left join pft_roundseat r on d.seat_id=r.id where d.id in ($dync_ids)");
		$sinfo_a = array();
		foreach($dbRows as $key=>$row) $sinfo_a[] = $row['custom_num'];
		$sinfo = implode('_', $sinfo_a);
		
		// 获取分区名称
		$dbRows = \PFT\Db::fetch("select zone_name from pft_roundzone where id={$_REQUEST['AreaID']} limit 1");
		$zone_name = $dbRows['zone_name'];
		
		$text = "200|演出时间：".$rinfo_1."，座位分区：".$zone_name."，座位号：".implode('，', $sinfo_a)."。|$rinfo|$sinfo";
		
	}else{
		
		// 没有分配分区无需座位
		$text = "200|演出时间：".$rinfo_1."|$rinfo|$sinfo";
	}
		
	\PFT\Db::Close();
	Response($text, 200);
	
}elseif($_REQUEST['Action']=='MOD'){

	$fid      = $_REQUEST['Fid'];
	$tnum     = $_REQUEST['Tnum'];   // 修改后的数量 0 取消 -1 不做修改
	$ordernum = $_REQUEST['Ordern']; // 预定订单号
	$round_id = $_REQUEST['RoundID'];// 预定场次ID
	$zone_id  = $_REQUEST['AreaID']; // 分区ID
	
	if($tnum!=-1){
		
		$xml = $s->Order_Globle_Search("", $fid,"","","","","","","","","","", $ordernum,"","","","","",1,1,0,1,"","","","","","","","", "",0,1);
		$simxml = simplexml_load_string($xml);
		$sTnum = (string)$simxml->Rec->UUtnum;
		$ticket_num = $sTnum - $tnum;
		if($tnum==0) $ticket_num = $sTnum;// 取消所有
		
		// 释放座位失败
		if(!$ph->release_order_seat($ordernum, $ticket_num)) Response("订单:$ordernum,释放座位数:$ticket_num,失败.", 201, '201|座位释放失败');
	}


	// 获取剩余信息
	$sql = "select d.*,s.custom_num,r.use_date,r.bt,r.et from pft_roundseat_dyn d left join pft_roundseat s on d.seat_id=s.id left join pft_round r on d.round_id=r.id where d.ordernum=$ordernum and d.status=3";
	$dbRows = \PFT\Db::fetchAll($sql);
	$sinfo_a = array(); $rinfo = '';
	foreach($dbRows as $key=>$row){
		$sinfo_a[] = $row['custom_num'];
		if($rinfo==''){
			$rinfo  = $row['use_date'].' '.$row['bt'].'-'.$row['et'];
			$rinfo_1= $row['bt'];
		}
	}
	
	$text = "200|演出时间：".$rinfo_1."。|$rinfo|$sinfo";
	
	if($zone_id>0){// 获取分区名称
		$sinfo = implode('_', $sinfo_a);
		$dbRows = \PFT\Db::fetch("select zone_name from pft_roundzone where id=$zone_id limit 1");
		$zone_name = $dbRows['zone_name'];
		$text = "200|演出时间：".$rinfo_1."，座位分区：".$zone_name."，座位号：".implode('，', $sinfo_a)."。|$rinfo|$sinfo";
	}
	
	\PFT\Db::Close();
	Response("MOD:".$text, 200, $text);
	
}elseif($_REQUEST['Action']=='Relation'){// 更新座位关联

	
	$tnum     = $_REQUEST['Tnum'];   // 预定座位数
	$buyerid  = $_REQUEST['Fid'];    // 预定账号
	$ordernum = $_REQUEST['Ordern']; // 预定订单号
	$round_id = $_REQUEST['RoundID'];// 预定场次ID
	$zone_id  = $_REQUEST['AreaID']; // 分区ID
	$status   = 3;
	
	if($_REQUEST['PayStatus']==2) $status = 2;// 未支付更新但状态是锁定
	
	$result = $ph->update_seat_order($ordernum, $buyerid, $tnum, $round_id, $zone_id, $status);
	Response("订单座位关联:$ordernum|$buyerid|$status|$result", 200,"200|success");

}elseif($_REQUEST['Action']=='ModSuccess'){
	// $buyerid  = $_REQUEST['Fid'];
	// $ordernum = $_REQUEST['Ordern'];
	// $option = array("","","","","","","","","","","",$ordernum,"","","","","","",1,1,0,1,"","","","","","","","","",0,0);
	// $resxml = $s->__soapCall('Order_Globle_Search', $option);
	// $simxml = simplexml_load_string($resxml);
	// if((int)$simxml->Rec->UUpaystatus==2){
		// write_logs("---upd--$ordernum|$ordernum|$buyerid|未支付");
		// exit("200|success");
	// }
	
	
	// $result = $ph->update_seat_order($ordernum, $buyerid);
	// write_logs("---upd--$ordernum|$ordernum|$buyerid");
	// exit("200|success");
	// $buyerid  = $_REQUEST['Fid'];
	// $ordernum = $_REQUEST['ordernum'];
	// $result = $ph->update_seat_order($ordernum, $buyerid);
	// write_logs("---upd--$$ordernum|$ordernum|$buyerid");
	// exit("200|success");
}elseif($_REQUEST['Action']=='Relation_after_pay'){// 支付完成后调用更新

	$buyerid  = $_REQUEST['Fid'];
	$ordernum = $_REQUEST['Ordern'];	
	$result = $ph->update_seat_order_after_pay($ordernum, $buyerid);
	Response("$ordernum|$ordernum|$buyerid|$status", 200,"200|success");
	
}elseif($_REQUEST['Action']=='Release_dync_seat'){// 释放座位

	if(!$ph->release_dync_seat($_REQUEST['RoundID'], $_REQUEST['VenueID'], $_REQUEST['AreaID'], $_REQUEST['Tnum'], $_REQUEST['Fid'])){
		abc777\Syslog::write($_REQUEST['Fid'], 'show_product_seat_release.txt');
	}
	exit("200|success");
	
}



function write_logs($txt,$file="logs/hhhh.txt"){
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}


function Response($text, $status,$response=''){
	
	$month = date('Ym');
	if($status==200){
		write_logs($text, "logs/product_show_s_$month.txt");
		if($response!='') exit("$response");
		exit("$text");
	}
	write_logs($text, "logs/product_show_e_$month.txt");
	exit("$status|$text");
}